package com.test;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class CommandHelloWorldObserver extends HystrixObservableCommand<String> {

    private final String name;

    public CommandHelloWorldObserver(String name) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected Observable<String> construct() {

        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("服务1");
                subscriber.onNext(" 服务2");
                int a = 1/0;
                subscriber.onNext(" 服务3");
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io());

    }

    /**
     * 服务降级
     * @return
     */
    @Override
    protected Observable<String> resumeWithFallback() {
        return Observable.create(new Observable.OnSubscribe<String>(){

            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("失败了");
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io());
    }
}
